package me.mingshan.leetcode;

/**
 * @author hanjuntao
 * @date 2025/5/30 0030
 */
public class L_374_guess_number_higher_or_lower extends GuessGame {

    public static void main(String[] args) {
        L_374_guess_number_higher_or_lower solution = new L_374_guess_number_higher_or_lower();
        solution.setPick(2);
        System.out.println(solution.guessNumber(2));
    }

    /**
     * 采用二分法（左闭右闭）
     *
     * @param n
     * @return
     */
    public int guessNumber(int n) {
        if (n == 1) {
            return 1;
        }

        int left  = 1;
        int right = n;
        while (left <= right) {
            int mid = left + (right - left) / 2;

            int guess = guess(mid);

            if (guess == 0) {
                return mid;
            } else if (guess == -1) {
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }

        return -1;
    }
}
